package com.example.idachuappone.index.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.idachuappone.R;
import com.example.idachuappone.cook.entity.CookResult;
import com.example.idachuappone.index.item.CommentListActivity;
import com.example.idachuappone.utils.BitmapUtilHelper;
import com.example.idachuappone.utils.ComUtil;
import com.example.idachuappone.utils.MainToast;
import com.example.idachuappone.utils.Utils;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import com.lidroid.xutils.bitmap.callback.BitmapLoadCallBack;
import com.lidroid.xutils.bitmap.callback.BitmapLoadFrom;

public class CookAdapter extends BaseAdapter {
	private String breed;
	private BitmapUtils bu;
	private Context context;
	ViewHolder holder;
	private boolean is_nearby;
	private List<CookResult> list;
	private String location;

	class AnonymousClass_1 implements OnClickListener {
		private final/* synthetic */CookResult val$cookResult;

		AnonymousClass_1(CookResult cookResult) {
			this.val$cookResult = cookResult;
		}

		@Override
		public void onClick(View v) {
			Intent intent1 = new Intent(CookAdapter.this.context, CommentListActivity.class);
			Bundle bundle1 = new Bundle();
			bundle1.putSerializable("id", this.val$cookResult.getId());
			intent1.putExtras(bundle1);
			CookAdapter.this.context.startActivity(intent1);
		}
	}

	private class ViewHolder {
		ImageView img_cook;
		ImageView img_green;
		ImageView img_star;
		LinearLayout ll_comment;
		TextView tv_bespeak_num;
		TextView tv_breed;
		TextView tv_comment_content;
		TextView tv_comment_name;
		TextView tv_distance;
		TextView tv_green_name;
		TextView tv_name;

		private ViewHolder() {
		}
	}

	public void setLocation(String location) {
		this.location = location;
	}

	public void setBreed(String breed) {
		this.breed = breed;
	}

	public void setIs_nearby(boolean is_nearby) {
		this.is_nearby = is_nearby;
	}

	public CookAdapter(Context context, BitmapUtils bu) {
		this.is_nearby = true;
		this.holder = null;
		this.context = context;
		this.bu = bu;
	}

	public void setList(List<CookResult> list) {
		this.list = list;
	}

	@Override
	public int getCount() {
		return this.list == null ? 0 : this.list.size();
	}

	@Override
	public Object getItem(int position) {
		return this.list == null ? null : (CookResult) this.list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		CookResult cookResult = this.list.get(position);
		if (convertView == null) {
			convertView = LinearLayout.inflate(this.context, R.layout.simple_index_item, null);
			this.holder = new ViewHolder();
			this.holder.img_cook = (ImageView) convertView.findViewById(R.id.img_cook);
			this.holder.img_green = (ImageView) convertView.findViewById(R.id.img_green);
			this.holder.img_star = (ImageView) convertView.findViewById(R.id.img_star);
			this.holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
			this.holder.tv_breed = (TextView) convertView.findViewById(R.id.tv_breed);
			this.holder.ll_comment = (LinearLayout) convertView.findViewById(R.id.ll_comment);
			this.holder.tv_green_name = (TextView) convertView.findViewById(R.id.tv_green_name);
			this.holder.tv_bespeak_num = (TextView) convertView.findViewById(R.id.tv_bespeak_num);
			this.holder.tv_distance = (TextView) convertView.findViewById(R.id.tv_distance);
			this.holder.tv_comment_content = (TextView) convertView.findViewById(R.id.tv_comment_content);
			this.holder.tv_comment_name = (TextView) convertView.findViewById(R.id.tv_comment_name);
			convertView.setTag(this.holder);
		} else {
			this.holder = (ViewHolder) convertView.getTag();
		}
		if (cookResult.getComment() != null) {
			Log.e("a", cookResult.getComment().getContent());
			this.holder.tv_comment_content.setText(cookResult.getComment().getContent());
			if (cookResult.getComment().getUser() == null || cookResult.getComment().getUser().getName() == null
					|| cookResult.getComment().getUser().getName().trim().length() <= 0) {
				this.holder.tv_comment_name.setText("\u533f\u540d\uff1a");
			} else {
				this.holder.tv_comment_name.setText(new StringBuilder(String.valueOf(cookResult.getComment().getUser()
						.getName())).append("\uff1a").toString());
			}
			this.holder.ll_comment.setOnClickListener(new AnonymousClass_1(cookResult));
		} else {
			this.holder.tv_comment_name.setText("");
			this.holder.tv_comment_content
					.setText("\u8fd8\u6ca1\u4eba\u8bc4\u8bba\u8fc7\u5462\uff0c\u5feb\u6765\u9884\u7ea6\u5427~");
			this.holder.ll_comment.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					MainToast.show(CookAdapter.this.context,
							"\u8fd8\u6ca1\u4eba\u8bc4\u8bba\u8fc7\u5462\uff0c\u5feb\u6765\u9884\u7ea6\u5427~", 0);
				}
			});
		}
		String str = "";
		if (!this.is_nearby) {
			str = ComUtil.getTwoSum(cookResult.getLocation(), this.location);
		} else if (this.location == null || this.location.length() <= 0) {
			str = "\u65e0\u6cd5\u83b7\u53d6\u672c\u5730\u4f4d\u7f6e";
		} else {
			str = ComUtil.getTwoSum(cookResult.getLocation(), this.location);
		}
		this.holder.tv_distance.setText(str);
		this.holder.tv_name.setText(cookResult.getName());
		if (this.breed != null && this.breed.trim().length() > 0) {
			this.holder.tv_breed.setText("\u64c5\u957f" + this.breed);
		} else if (cookResult.getTags() == null || cookResult.getTags().size() <= 0) {
			this.holder.tv_breed.setText("\u64c5\u957f\u5bb6\u5e38\u83dc");
		} else {
			this.holder.tv_breed.setText("\u64c5\u957f" + cookResult.getTags().get(0).getName());
		}
		this.holder.tv_bespeak_num.setText(cookResult.getBespeak_num());
		this.bu.display(this.holder.img_green, cookResult.getRecipe().get(0).getImg());
		this.holder.tv_green_name.setText(cookResult.getRecipe().get(0).getName());
		this.bu.display(this.holder.img_cook, cookResult.getHead_small(), new BitmapLoadCallBack<View>() {
			@Override
			public void onLoadCompleted(View view, String arg1, Bitmap bitmap, BitmapDisplayConfig arg3,
					BitmapLoadFrom arg4) {
				((ImageView) view).setImageBitmap(BitmapUtilHelper.getCroppedRoundBitmap(bitmap,
						Utils.dip2px(CookAdapter.this.context, 28.0f)));
			}

			@Override
			public void onLoadFailed(View arg0, String arg1, Drawable arg2) {
			}
		});
		if (Double.valueOf(cookResult.getStar()).doubleValue() > 4.0d) {
			this.holder.img_star.setBackgroundResource(R.drawable.icon_star05);
		} else if (Double.valueOf(cookResult.getStar()).doubleValue() > 3.0d) {
			this.holder.img_star.setBackgroundResource(R.drawable.icon_star04);
		} else if (Double.valueOf(cookResult.getStar()).doubleValue() > 2.0d) {
			this.holder.img_star.setBackgroundResource(R.drawable.icon_star03);
		} else if (Double.valueOf(cookResult.getStar()).doubleValue() > 1.0d) {
			this.holder.img_star.setBackgroundResource(R.drawable.icon_star02);
		} else {
			this.holder.img_star.setBackgroundResource(R.drawable.icon_star01);
		}
		return convertView;
	}
}